<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="boxName">
              <el-input v-model="filters.boxName" clearable placeholder="设备名称"></el-input>
            </el-form-item>
            <el-form-item prop="boxCode">
              <el-input v-model="filters.boxCode" clearable placeholder="设备编号"></el-input>
            </el-form-item>
            <el-form-item prop="iccid">
              <el-input v-model="filters.iccid" clearable placeholder="卡iccid"></el-input>
            </el-form-item>
            <el-form-item>
              <el-select v-model="filters.boxStatus" filterable :clearable="true" placeholder="设备状态">
                <el-option v-for="item in boxStatusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="filters.openStatus" filterable :clearable="true" placeholder="锁状态">
                <el-option v-for="item in openStatusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="filters.coldStatus" filterable :clearable="true" placeholder="制冷状态">
                <el-option v-for="item in coldStatusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="filters.warnStatus" filterable :clearable="true" placeholder="报警状态">
                <el-option v-for="item in warnStatusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>

      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" align="center" width="60"></el-table-column>
        <el-table-column prop="boxName" label="设备名称"></el-table-column>
        <el-table-column prop="boxCode" label="设备编号"></el-table-column>
        <el-table-column prop="temp" :formatter="formatTemp" label="设备温度"></el-table-column>
        <!--<el-table-column prop="demp" :formatter="formatDemp" label="设备湿度"></el-table-column>-->
        <el-table-column prop="addr" :formatter="formatNull" label="设备位置" width="150"></el-table-column>
        <el-table-column prop="power" :formatter="formatNull" label="剩余电量"></el-table-column>
        <el-table-column prop="tilt" :formatter="formatTilt" label="倾斜度"></el-table-column>
        <el-table-column prop="syUserName" label="收样员"></el-table-column>
        <el-table-column label="设备状态">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="formatBoxs(scope.row.boxStatus)">
                {{ formatBox(scope.row.boxStatus) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="锁状态" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="plain" :type="isTrueType(scope.row.openStatus)">
                {{ formatOpen(scope.row.openStatus) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="制冷状态" align="center">
          <span>无</span>
          <!--<template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="plain" :type="isTrueType(scope.row.coldStatus)">
              </el-tag>
            </span>
          </template> -->
        </el-table-column>
        <el-table-column label="报警状态">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatWarns(scope.row.warnStatus)">
                {{ formatWarn(scope.row.warnStatus) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="latestTime" :formatter="formatNull" label="最新上报时间" width="150"></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button v-has="permissionLog" @click="checkBoxLog(scope.row)" size="mini">心跳记录</el-button>
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                <el-dropdown-item v-has="permissionRecord" @click.native="getFingerprintList(scope.row)">指纹列表</el-dropdown-item>
                <el-dropdown-item v-has="permissionUser" @click.native="checkBoxRecord(scope.row)">操作记录</el-dropdown-item>
                <el-dropdown-item v-has="permissionInstruct" v-if="scope.row.boxStatus == 3" @click.native="handleClickInstructions(scope.row, 120)">开启心跳</el-dropdown-item>
                <div v-has="permissionInstruct" v-if="scope.row.boxStatus == 1">
                  <el-dropdown-item @click.native="handleClickInstructions(scope.row, 121)">关闭心跳</el-dropdown-item>
                  <el-dropdown-item @click.native="handleClickInstructions(scope.row, 100)">重置配置</el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.coldStatus == 0" @click.native="handleClickInstructions(scope.row, 122)">开启制冷</el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.coldStatus == 1" @click.native="handleClickInstructions(scope.row, 123)">关闭制冷</el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.openStatus == 0" @click.native="handleClickInstructions(scope.row, 124)">开锁</el-dropdown-item>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <!-- 查看列表的弹出框 -->
    <el-dialog :title="dialogTit" :visible.sync="showDialog" :before-close="closeDialog" width="60%">
      <div v-if="tableType == 1">
        <el-table :data="logData" :fit="true" highlight-current-row>
          <!-- 记录表内容 -->
          <el-table-column prop="createDate" label="创建时间"></el-table-column>
          <el-table-column prop="temp" :formatter="formatTemp" label="温度" width="80" align="center"></el-table-column>
          <el-table-column label="锁状态" width="100" align="center">
            <template slot-scope="scope">
              <span>
                <el-tag size="mini" effect="plain" :type="isTrueType(scope.row.openStatus)">
                  {{ formatOpen(scope.row.openStatus) }}
                </el-tag>
              </span>
            </template>
          </el-table-column>
          <!--<el-table-column prop="damp" :formatter="formatDemp" label="湿度" width="60" align="center"></el-table-column>-->
          <el-table-column prop="power" label="电量" width="80" align="center"></el-table-column>
          <el-table-column prop="tilt" :formatter="formatTilt" label="倾斜度" width="100" align="center"></el-table-column>
          <el-table-column prop="addr" label="位置" width="180"></el-table-column>
          <el-table-column label="报警状态" width="100" align="center">
            <template slot-scope="scope">
              <span>
                <el-tag size="mini" :type="formatWarns(scope.row.warnStatus)">
                  {{ formatWarn(scope.row.warnStatus) }}
                </el-tag>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="制冷状态" width="100" align="center">
            <span>无</span>
            <!--<template slot-scope="scope">
              <span>
                <el-tag size="mini" effect="plain" :type="isTrueType(scope.row.coldStatus)">

                </el-tag>
              </span>
            </template>-->
          </el-table-column>
          <el-table-column label="元件异常状态" width="100" align="center">
            <template slot-scope="scope">
              <span>
                <el-tag size="mini" effect="dark" :type="formatErrors(scope.row.errorStatus)">
                  {{ formatError(scope.row.errorStatus) }}
                </el-tag>
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-if="tableType == 2">
        <el-table :data="recordData" :fit="true" highlight-current-row>
          <!-- 操作表 -->
          <el-table-column prop="cmd" :formatter="formatCMD" label="指令类型" width="100"></el-table-column>
          <el-table-column label="指令状态" width="70" align="center">
            <template slot-scope="scope">
              <span>
                <el-tag size="mini" effect="dark" :type="formatCmdStatuss(scope.row.cmdStatus)">
                  {{ formatCmdStatus(scope.row.cmdStatus) }}
                </el-tag>
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="timestamp" label="指令操作序号" width="120"></el-table-column>
          <el-table-column prop="dataJson" label="数据内容" width="200"></el-table-column>
          <el-table-column prop="fid" :formatter="formatNull" label="指纹ID"></el-table-column>
          <el-table-column prop="createUserName" label="创建人" width="90"></el-table-column>
          <el-table-column prop="createDate" label="创建时间"></el-table-column>
          <!--<el-table-column prop="modifyDate" label="更新时间"></el-table-column>-->
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import EquipApi from '@/api/equip/equip';

export default {
  name: 'BoxManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      filters: {
        iccid: '',
        boxName: '',
        boxCode: '',
        boxStatus: '',
        openStatus: '',
        coldStatus: '',
        warnStatus: '',
      },
      boxStatusList: [
        { value: 0, label: '未连接' },
        { value: 1, label: '已连接' },
        { value: 2, label: '已断开' },
        { value: 3, label: '已休眠' },
      ],
      openStatusList: [
        { value: 0, label: '关' },
        { value: 1, label: '开' },
      ],
      coldStatusList: [
        { value: 0, label: '未制冷' },
        { value: 1, label: '制冷中' },
      ],
      warnStatusList: [
        { value: 0, label: '正常' },
        { value: 1, label: '低温' },
        { value: 2, label: '高温' },
        { value: 3, label: '倾斜' },
        { value: 4, label: '锁未关' },
      ],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      tableType: '',
      dialogTit: '',
      showDialog: false,
      recordData: [], // 操作表
      logData: [], // 记录表
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_SEARCH',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_DELETE',
      permissionInstruct: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_INSTRUCT',
      permissionLog: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_LOG',
      permissionRecord: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_RECORD',
      permissionUser: 'SYSTEM_MANAGER_EQUIP_BOX_LIST_USER',
      timer: null, //用来接收setInterval()返回的 ID 值
    };
  },
  mounted() {
    let that = this;
    this.init();
    window.addEventListener('keydown', this.keyDown);
    function freshTable() {
      that.init();
      //console.log("成功调用定时任务")
    }
    that.timer = setInterval(freshTable, 30000);
  },

  // 退出当前界面 摧毁定时接口
  destroyed() {
    clearInterval(this.timer);
    this.timer = null;
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      this.listData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    // 获取列表数据
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      var _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        boxName: _this.filters.boxName,
        boxCode: _this.filters.boxCode,
        iccid: _this.filters.iccid,
        boxStatus: _this.filters.boxStatus,
        openStatus: _this.filters.openStatus,
        coldStatus: _this.filters.coldStatus,
        warnStatus: _this.filters.warnStatus,
      };
      EquipApi.listEquipBox(cond).then((data) => {
        this.dataList = data.data.list;
        this.page.pageTotal = data.total;
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    handleClickAdd() {
      // 新增
      this.$router.push('/equip/boxAdd');
    },
    // 获取指纹列表
    getFingerprintList(val) {
      this.$router.push({
        path: '/equip/boxFingerprintManagement',
        query: { id: val.boxId },
      });
    },
    // 查看详情
    handleClickDetail(val) {
      this.$router.push({
        path: '/equip/boxDetail',
        query: { id: val.boxId },
      });
    },
    // 编辑
    handleClickEditor(val) {
      let _this = this;
      _this.$router.push({
        path: '/equip/boxEdit',
        query: { id: val.boxId },
      });
    },
    // 删除
    handleClickDelete(val) {
      this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        let cond = {
          id: val.boxId,
          state: -1,
        };
        EquipApi.deleteEquipBox(cond).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickInstructions(row, val) {
      // 统一下发指令
      var tit = '';
      if (val == 100) {
        tit = '是否重置该标本箱子所有配置？';
      } else if (val == 120) {
        tit = '是否开启该标本数据上报？';
      } else if (val == 121) {
        tit = '是否关闭该标本数据上报？';
      } else if (val == 122) {
        tit = '是否开启该标本箱制冷？';
      } else if (val == 123) {
        tit = '是否关闭该标本箱制冷？';
      } else if (val == 124) {
        tit = '是否为该标本箱开锁？';
      }
      var cond = {
        id: row.boxId,
        cmd: val,
      };
      this.$confirm(tit, message.TITLE_PROMPT, {}).then(() => {
        EquipApi.changeEquipCmd(cond).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    closeDialog(done) {
      this.boxName = '';
      this.recordData = [];
      this.logData = [];
      done();
    },
    checkBoxLog(val) {
      // 心跳日志
      var cond = {
        boxId: val.boxId,
      };
      EquipApi.getEquipBoxLogTable(cond).then((res) => {
        this.tableType = 1;
        this.logData = res.data;
        this.dialogTit = val.boxName + ' - 心跳日志';
        this.showDialog = true;
      });
    },
    checkBoxRecord(val) {
      // 操作纪录
      var cond = {
        boxId: val.boxId,
      };
      EquipApi.getEquipBoxRecordTable(cond).then((res) => {
        this.tableType = 2;
        this.recordData = res.data;
        this.dialogTit = val.boxName + ' - 操作记录';
        this.showDialog = true;
      });
    },

    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        // 浏览器尺寸变化响应事件
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },

    // 过滤器
    formatNull(row, column, cellValue) {
      if (cellValue) {
        return cellValue;
      } else {
        return ' - ';
      }
    },
    formatTemp(row, column, cellValue) {
      if (cellValue) {
        return cellValue + ' ℃';
      } else {
        return ' - ';
      }
    },
    formatDemp(row, column, cellValue) {
      if (cellValue) {
        return cellValue + ' RH';
      } else {
        return ' - ';
      }
    },
    formatTilt(row, column, cellValue) {
      if (cellValue) {
        return cellValue + ' °';
      } else {
        return ' - ';
      }
    },
    formatCmdStatus(cellValue) {
      if (cellValue == 0) {
        return '未响应';
      } else if (cellValue == 1) {
        return '成功';
      } else if (cellValue == 2) {
        return '失败';
      }
    },
    formatCmdStatuss(cellValue) {
      if (cellValue == 0) {
        return '';
      } else if (cellValue == 1) {
        return 'success';
      } else if (cellValue == 2) {
        return 'info';
      }
    },
    formatBox(cellValue) {
      if (cellValue == 0) {
        return '未连接';
      } else if (cellValue == 1) {
        return '已连接';
      } else if (cellValue == 2) {
        return '已断开';
      } else if (cellValue == 3) {
        return '已休眠';
      }
    },
    formatBoxs(cellValue) {
      if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      } else if (cellValue == 2) {
        return 'info';
      } else if (cellValue == 3) {
        return '';
      }
    },
    formatCMD(row, column, cellValue) {
      if (cellValue == 1) {
        return '连接';
      } else if (cellValue == 2) {
        return '获取设备配置';
      } else if (cellValue == 3) {
        return '心跳上报数据';
      } else if (cellValue == 4) {
        return '开锁';
      } else if (cellValue == 5) {
        return '关锁';
      } else if (cellValue == 6) {
        return '元件异常';
      } else if (cellValue == 7) {
        return '上报变动蓝牙';
      } else if (cellValue == 8) {
        return '上报所有蓝牙';
      } else if (cellValue == 100) {
        return '重置配置';
      } else if (cellValue == 105) {
        return '语音播报';
      } else if (cellValue == 110) {
        return '新增指纹';
      } else if (cellValue == 111) {
        return '删除指纹';
      } else if (cellValue == 112) {
        return '获取所有指纹';
      } else if (cellValue == 113) {
        return '删除所有指纹';
      } else if (cellValue == 114) {
        return '禁用所有指纹';
      } else if (cellValue == 115) {
        return '启用指纹';
      } else if (cellValue == 120) {
        return '开启数据上报';
      } else if (cellValue == 121) {
        return '关闭数据上报';
      } else if (cellValue == 122) {
        return '开启制冷';
      } else if (cellValue == 123) {
        return '关闭制冷';
      } else if (cellValue == 124) {
        return '开锁';
      }
    },
    formatWarn(cellValue) {
      if (cellValue == 0) {
        return '正常';
      } else if (cellValue == 1) {
        return '低温警告';
      } else if (cellValue == 2) {
        return '高温警告';
      } else if (cellValue == 3) {
        return '倾斜警告';
      } else if (cellValue == 3) {
        return '锁未关警告';
      }
    },
    formatWarns(cellValue) {
      if (cellValue == 0) {
        return 'success';
      } else if (cellValue == 1) {
        return '';
      } else if (cellValue == 2) {
        return 'danger';
      } else if (cellValue == 3) {
        return 'warning';
      } else if (cellValue == 3) {
        return 'warning';
      }
    },
    formatError(cellValue) {
      if (cellValue == 1) {
        return '指纹锁异常';
      } else if (cellValue == 2) {
        return '制冷片异常';
      } else if (cellValue == 3) {
        return '温度异常';
      } else if (cellValue == 4) {
        return '通讯异常';
      } else if (cellValue == 5) {
        return '倾斜度异常';
      } else if (cellValue == 6) {
        return '语音异常';
      } else if (cellValue == 0) {
        return '无异常';
      }
    },
    formatErrors(cellValue) {
      if (cellValue == 0) {
        return 'success';
      } else {
        return 'warning';
      }
    },
    formatOpen(cellValue) {
      if (cellValue == 0) {
        return '关';
      } else if (cellValue == 1) {
        return '开';
      } else {
        return ' - ';
      }
    },
    formatCold(cellValue) {
      if (cellValue == 0) {
        return '未制冷';
      } else if (cellValue == 1) {
        return '制冷中';
      } else {
        return ' - ';
      }
    },
    isTrueType(cellValue) {
      if (cellValue == 0) {
        return 'warning';
      } else if (cellValue == 1) {
        return 'success';
      } else {
        return 'info';
      }
    },

    formatDate(row, column, cellValue) {
      return row.createDate;
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
<style scoped>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
